package com.xiaoyu.izf.controller;

import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.GoodsOrder;
import com.xiaoyu.izf.model.UserAddress;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.service.GoodsOrderService;
import com.xiaoyu.izf.service.UserAddressService;
import com.xiaoyu.izf.service.VipOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/goods/order")
public class GoodsOrderController extends BaseController {

    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private UserAddressService userAddressService;

    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage(){
        return "mgr/goods_order/goods_order_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(HttpServletRequest request, @RequestBody String body) throws IOException {
        UserInfo user = user(request);
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = goodsOrderService.list(paramListBean,user);
        return dataTableBean;
    }

    /**
     * 合伙人商品订单页面
     * @return
     */
    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parlistPage(){
        return "mgr/goods_order/par_goods_order_list";
    }

    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(HttpServletRequest request, @RequestBody String body) throws IOException {
        UserInfo user = user(request);
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = goodsOrderService.list(paramListBean,user);
        return dataTableBean;
    }

    /**
     * 出库
     * @param id
     * @return
     */
    @RequestMapping("/ex_warehouse")
    @ResponseBody
    @RequestAuth
    public FormResultBean exWarehouse(String id){
        FormResultBean resultBean = new FormResultBean();
        GoodsOrder order = goodsOrderService.findOne(id);
        if (order.getExWarehouse()!=null&&order.getExWarehouse()==1)
            return resultBean.setMessage("error").setMessage("已出库");
        order.setExWarehouse(1);
        goodsOrderService.save(order);
        return resultBean.setType("success").setMessage("操作成功");
    }

    /**
     * 发货
     * @param id
     * @return
     */
    @RequestMapping("/send_out")
    @ResponseBody
    @RequestAuth
    public FormResultBean sendout(String id){
        FormResultBean resultBean = new FormResultBean();
        GoodsOrder order = goodsOrderService.findOne(id);
        if (order.getSendOut()!=null&&order.getSendOut()==1)
            return resultBean.setMessage("error").setMessage("已发货");
        order.setSendOut(1);
        goodsOrderService.save(order);
        return resultBean.setType("success").setMessage("操作成功");
    }


    /**
     * 收件人信息
     * @param id
     * @return
     */
    @RequestMapping("/addressee_page")
    @RequestAuth
    public String addresseePage(String id, Model model){
        GoodsOrder order = goodsOrderService.findOne(id);
        UserAddress address = userAddressService.findOne(order.getUid());
        model.addAttribute("userAdd",address);
        return "mgr/goods_order/addressee";
    }
}
